//
//  YHYFavoriteViewController.m
//  TTMusic
//
//  Created by yanghuiyan on 16/4/1.
//  Copyright © 2016年 cpy. All rights reserved.
//

#import "YHYFavoriteViewController.h"
#import "CPYMVTableViewCell.h"
#import "CPYMVPlayViewController.h"
@interface YHYFavoriteViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) TTMusicBaseTableView *myPageTableView;

@property (nonatomic, strong) NSMutableArray *dataSource;

@property (nonatomic, strong) MBProgressHUD *HUD;

@property (nonatomic, strong) UIImageView *emptyImageview;

@end

@implementation YHYFavoriteViewController

#pragma mark - 收藏页面
- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    
    self.HUD.hidden = YES;
    
    self.dataSource = [NSMutableArray array];
    
    self.dataSource = [[YHY_DataBaseTool shareDataBase] selectCPYMVModelTable];
    
    // 即将进入页面时 判断是否有数据 若没有则附上背景图
    
    if (self.dataSource.count == 0) {
        
        self.myPageTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        self.emptyImageview =[[UIImageView alloc] initWithFrame:CGRectMake((SysWidth *20)/375, (SysHeight *200)/667, (SysWidth *335)/375, (SysHeight *335)/667)];
        
        self.emptyImageview.image = [UIImage imageNamed:@"10"];
        
        [self.view addSubview:self.emptyImageview];
        
        // 否则隐藏
    }else{
        
        self.emptyImageview.hidden = YES;
        
    }
    
    // 加载tableView
    [self.myPageTableView reloadData];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    //导航栏左侧按钮
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"返回"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ] style:UIBarButtonItemStylePlain target:self action:@selector(leftItemAction:)];
    self.navigationItem.leftBarButtonItem = leftItem;
    
    //自定义标题视图
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 40)];
    titleLabel.text = @"MV 收藏";
    titleLabel.font = [UIFont boldSystemFontOfSize:25];
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    self.navigationItem.titleView = titleLabel;
    
    
    self.myPageTableView = [[TTMusicBaseTableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    [self.myPageTableView registerClass:[CPYMVTableViewCell class] forCellReuseIdentifier:@"YHY_Favorite_TVCellIdentifier"];
    self.myPageTableView.delegate = self;
    self.myPageTableView.dataSource = self;
    [self.view addSubview:self.myPageTableView];
    
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
}

#pragma mark - 左侧按钮点击事件
- (void) leftItemAction:(UIBarButtonItem *)button {
    
    [self.navigationController popViewControllerAnimated:YES];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"shuaxin" object:nil];
    
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CPYMVTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YHY_Favorite_TVCellIdentifier"];
    cell.shareModel = self.dataSource[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 300;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    CPYMVModel *mvModel = self.dataSource[indexPath.row];
    
    [[CPYMVPlayViewController shareMVPlayViewController] playWithSongMVURL:mvModel.url];
    
    [self presentViewController:[CPYMVPlayViewController shareMVPlayViewController] animated:YES completion:^{
    }];

    

}

#pragma mark - 删除
//重写系统提供按钮的方法
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    //首先让父类调用
    [super setEditing:editing animated:animated];
    //让tableView处于编辑状态下
    [self.myPageTableView setEditing:YES animated:YES];
}

#pragma mark - 编辑tableView
//设置删除样式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleDelete;
}

//是否允许编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

//完成编辑
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
        CPYMVModel *mvModel = self.dataSource[indexPath.row];
        // 先移除数据 再删除行
        [self.dataSource removeObjectAtIndex:indexPath.row];
        [self.myPageTableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationMiddle];
        [[YHY_DataBaseTool shareDataBase] deleteCPYMVModelTable:mvModel.bigPicUrl];
        
        self.dataSource = [[YHY_DataBaseTool shareDataBase] selectCPYMVModelTable];
        
        if (self.dataSource.count == 0) {
            
            self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
            
            //设置类型
            self.HUD.mode = MBProgressHUDModeText;
            
            //设置文字
            self.HUD.labelText = @"暂时没有收藏了┗|｀O′|┛ 嗷~~";
           
        }
        
        
        
        [self.myPageTableView reloadData];

    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
